package com.example.springbootjspshiro.service.Impl;

import com.example.springbootjspshiro.mapper.UserMapper;
import com.example.springbootjspshiro.pojo.Perms;
import com.example.springbootjspshiro.pojo.Role;
import com.example.springbootjspshiro.pojo.User;
import com.example.springbootjspshiro.service.UserService;
import com.example.springbootjspshiro.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional//处理事务
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    public void Register(User user){//前端来的数据
        //处理层要对注册的密码进行加密
        //1、生成随机盐
        String salt = SaltUtils.getSalt(8);
        //2、加入
        user.setSalt(salt);
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt);
        user.setPassword(md5Hash.toHex());
        userMapper.Register(user);
    }

    @Override
    public User Login(String username) {
        User loginUser = userMapper.Login(username);
        return loginUser;
    }

    @Override
    public List<Role> findRolesByUserName(String username) {
         List<Role> rolesByUsername = userMapper.findRolesByUsername(username);
        return rolesByUsername;
    }

    @Override
    public User findUseRolesByUsername(String username) {
        return userMapper.findUseRolesByUsername(username);
    }

    @Override
    public List<Perms> findPermsByRoleId(Integer RoleId) {
        return userMapper.findPermsByRoleId(RoleId);
    }
}
